package org.jivesoftware.smackx.provider;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smackx.packet.VCard;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

public class VCardProvider
  implements IQProvider
{
  private static final String PREFERRED_ENCODING = "UTF-8";

  public static VCard createVCardFromXML(String paramString)
    throws Exception
  {
    VCard localVCard = new VCard();
    new VCardReader(localVCard, DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(paramString.getBytes("UTF-8")))).initializeFields();
    return localVCard;
  }

  // ERROR //
  public org.jivesoftware.smack.packet.IQ parseIQ(org.xmlpull.v1.XmlPullParser paramXmlPullParser)
    throws Exception
  {
    // Byte code:
    //   0: new 65	java/lang/StringBuilder
    //   3: dup
    //   4: invokespecial 66	java/lang/StringBuilder:<init>	()V
    //   7: astore_2
    //   8: aload_1
    //   9: invokeinterface 72 1 0
    //   14: istore 5
    //   16: goto +130 -> 146
    //   19: iload 5
    //   21: iconst_3
    //   22: if_icmpne +112 -> 134
    //   25: ldc 74
    //   27: aload_1
    //   28: invokeinterface 78 1 0
    //   33: invokevirtual 82	java/lang/String:equals	(Ljava/lang/Object;)Z
    //   36: istore 8
    //   38: iload 8
    //   40: ifeq +94 -> 134
    //   43: aload_2
    //   44: invokevirtual 85	java/lang/StringBuilder:toString	()Ljava/lang/String;
    //   47: invokestatic 87	org/jivesoftware/smackx/provider/VCardProvider:createVCardFromXML	(Ljava/lang/String;)Lorg/jivesoftware/smackx/packet/VCard;
    //   50: areturn
    //   51: aload_2
    //   52: aload_1
    //   53: invokeinterface 90 1 0
    //   58: invokestatic 96	org/jivesoftware/smack/util/StringUtils:escapeForXML	(Ljava/lang/String;)Ljava/lang/String;
    //   61: invokevirtual 100	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
    //   64: pop
    //   65: goto -46 -> 19
    //   68: astore 4
    //   70: aload 4
    //   72: invokevirtual 103	org/xmlpull/v1/XmlPullParserException:printStackTrace	()V
    //   75: goto -32 -> 43
    //   78: aload_2
    //   79: bipush 60
    //   81: invokevirtual 106	java/lang/StringBuilder:append	(C)Ljava/lang/StringBuilder;
    //   84: aload_1
    //   85: invokeinterface 78 1 0
    //   90: invokevirtual 100	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
    //   93: bipush 62
    //   95: invokevirtual 106	java/lang/StringBuilder:append	(C)Ljava/lang/StringBuilder;
    //   98: pop
    //   99: goto -80 -> 19
    //   102: astore_3
    //   103: aload_3
    //   104: invokevirtual 107	java/io/IOException:printStackTrace	()V
    //   107: goto -64 -> 43
    //   110: aload_2
    //   111: ldc 109
    //   113: invokevirtual 100	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
    //   116: aload_1
    //   117: invokeinterface 78 1 0
    //   122: invokevirtual 100	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
    //   125: bipush 62
    //   127: invokevirtual 106	java/lang/StringBuilder:append	(C)Ljava/lang/StringBuilder;
    //   130: pop
    //   131: goto -112 -> 19
    //   134: aload_1
    //   135: invokeinterface 112 1 0
    //   140: istore 7
    //   142: iload 7
    //   144: istore 5
    //   146: iload 5
    //   148: tableswitch	default:+-129 -> 19, 2:+-70->78, 3:+-38->110, 4:+-97->51
    //
    // Exception table:
    //   from	to	target	type
    //   8	38	68	org/xmlpull/v1/XmlPullParserException
    //   51	65	68	org/xmlpull/v1/XmlPullParserException
    //   78	99	68	org/xmlpull/v1/XmlPullParserException
    //   110	142	68	org/xmlpull/v1/XmlPullParserException
    //   8	38	102	java/io/IOException
    //   51	65	102	java/io/IOException
    //   78	99	102	java/io/IOException
    //   110	142	102	java/io/IOException
  }

  private static class VCardReader
  {
    private final Document document;
    private final VCard vCard;

    VCardReader(VCard paramVCard, Document paramDocument)
    {
      this.vCard = paramVCard;
      this.document = paramDocument;
    }

    private void appendText(StringBuilder paramStringBuilder, Node paramNode)
    {
      NodeList localNodeList = paramNode.getChildNodes();
      int i = 0;
      while (true)
      {
        if (i >= localNodeList.getLength())
          return;
        Node localNode = localNodeList.item(i);
        String str = localNode.getNodeValue();
        if (str != null)
          paramStringBuilder.append(str);
        appendText(paramStringBuilder, localNode);
        ++i;
      }
    }

    private String getTagContents(String paramString)
    {
      String str;
      NodeList localNodeList = this.document.getElementsByTagName(paramString);
      if ((localNodeList != null) && (localNodeList.getLength() == 1))
        str = getTextContent(localNodeList.item(0));
      while (true)
      {
        return str;
        str = null;
      }
    }

    private String getTextContent(Node paramNode)
    {
      StringBuilder localStringBuilder = new StringBuilder();
      appendText(localStringBuilder, paramNode);
      return localStringBuilder.toString();
    }

    private boolean isWorkHome(String paramString)
    {
      int i;
      if ((!("HOME".equals(paramString))) && (!("WORK".equals(paramString))))
        i = 0;
      while (true)
      {
        return i;
        i = 1;
      }
    }

    private void setupAddresses()
    {
      Object localObject;
      ArrayList localArrayList1;
      ArrayList localArrayList2;
      NodeList localNodeList2;
      int j;
      int k;
      NodeList localNodeList1 = this.document.getElementsByTagName("ADR");
      if (localNodeList1 == null)
        return;
      int i = 0;
      while (true)
      {
        if (i >= localNodeList1.getLength());
        Element localElement = (Element)localNodeList1.item(i);
        localObject = null;
        localArrayList1 = new ArrayList();
        localArrayList2 = new ArrayList();
        localNodeList2 = localElement.getChildNodes();
        j = 0;
        if (j < localNodeList2.getLength())
          break;
        k = 0;
        if (k < localArrayList2.size())
          break label185;
        ++i;
      }
      Node localNode = localNodeList2.item(j);
      if (localNode.getNodeType() != 1);
      while (true)
      {
        String str;
        while (true)
        {
          while (true)
            ++j;
          str = localNode.getNodeName();
          if (!(isWorkHome(str)))
            break;
          localObject = str;
        }
        localArrayList1.add(str);
        localArrayList2.add(getTextContent(localNode));
      }
      if ("HOME".equals(localObject))
        label185: this.vCard.setAddressFieldHome((String)localArrayList1.get(k), (String)localArrayList2.get(k));
      while (true)
      {
        while (true)
          ++k;
        this.vCard.setAddressFieldWork((String)localArrayList1.get(k), (String)localArrayList2.get(k));
      }
    }

    private void setupEmails()
    {
      int i;
      NodeList localNodeList = this.document.getElementsByTagName("USERID");
      if (localNodeList == null);
      do
      {
        return;
        i = 0;
      }
      while (i >= localNodeList.getLength());
      Element localElement = (Element)localNodeList.item(i);
      if ("WORK".equals(localElement.getParentNode().getFirstChild().getNodeName()))
        this.vCard.setEmailWork(getTextContent(localElement));
      while (true)
      {
        while (true)
          ++i;
        this.vCard.setEmailHome(getTextContent(localElement));
      }
    }

    private void setupPhones()
    {
      int i;
      NodeList localNodeList1 = this.document.getElementsByTagName("TEL");
      if (localNodeList1 == null);
      do
      {
        return;
        i = 0;
      }
      while (i >= localNodeList1.getLength());
      NodeList localNodeList2 = localNodeList1.item(i).getChildNodes();
      Object localObject1 = null;
      Object localObject2 = null;
      String str1 = null;
      int j = 0;
      if (j >= localNodeList2.getLength())
        if ((localObject2 != null) && (str1 != null))
          break label161;
      while (true)
      {
        while (true)
        {
          while (true)
            ++i;
          Node localNode = localNodeList2.item(j);
          if (localNode.getNodeType() != 1);
          while (true)
          {
            String str2;
            while (true)
            {
              while (true)
              {
                while (true)
                  ++j;
                str2 = localNode.getNodeName();
                if (!("NUMBER".equals(str2)))
                  break;
                str1 = getTextContent(localNode);
              }
              if (!(isWorkHome(str2)))
                break;
              localObject1 = str2;
            }
            localObject2 = str2;
          }
          label161: if (!("HOME".equals(localObject1)))
            break;
          this.vCard.setPhoneHome(localObject2, str1);
        }
        this.vCard.setPhoneWork(localObject2, str1);
      }
    }

    private void setupSimpleFields()
    {
      Element localElement;
      String str;
      NodeList localNodeList = this.document.getDocumentElement().getChildNodes();
      int i = 0;
      if (i >= localNodeList.getLength())
        return;
      Node localNode = localNodeList.item(i);
      if (localNode instanceof Element)
      {
        localElement = (Element)localNode;
        str = localElement.getNodeName();
        if (localElement.getChildNodes().getLength() != 0)
          break label90;
        this.vCard.setField(str, "");
      }
      while (true)
      {
        label90: 
        do
          while (true)
            ++i;
        while ((localElement.getChildNodes().getLength() != 1) || (!(localElement.getChildNodes().item(0) instanceof Text)));
        this.vCard.setField(str, getTextContent(localElement));
      }
    }

    public void initializeFields()
    {
      this.vCard.setFirstName(getTagContents("GIVEN"));
      this.vCard.setLastName(getTagContents("FAMILY"));
      this.vCard.setMiddleName(getTagContents("MIDDLE"));
      this.vCard.setEncodedImage(getTagContents("BINVAL"));
      setupEmails();
      this.vCard.setOrganization(getTagContents("ORGNAME"));
      this.vCard.setOrganizationUnit(getTagContents("ORGUNIT"));
      setupSimpleFields();
      setupPhones();
      setupAddresses();
    }
  }
}